package com.dazhewan.project.ocr.strategy;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhengxuexue
 * @date 2023/12/15 16:32
 * @description 基于TesseractOcrStrategy实现的OCR识别策略
 */
public class TesseractOcrStrategy implements IOrcStrategyI {
    private String tessDataPath = "E:\\tessdata";
    private String chiness = "chi_sim";
    private String english = "eng";
    private String all = "eng+chi_sim";
    private String choose=chiness;

    public TesseractOcrStrategy(int operaCode) {
        switch (operaCode){
            case 49:
                this.choose=this.chiness;
                break;
            case 50:
                this.choose=this.english;
                break;
            case 51:
                this.choose=this.all;
                break;
        }
    }

    @Override
    public String identifyWordsByImage(BufferedImage bufferedImage) throws IOException {
        ITesseract instance = new Tesseract();
        //指定语言包的位置就是步骤1中解压的语言包
        instance.setDatapath(tessDataPath);
        // 我们需要指定识别语种+代表使用多语言识别
        instance.setLanguage(choose);
        try {
            String ocrResult = instance.doOCR(bufferedImage);
            return ocrResult;
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getTessDataPath() {
        return tessDataPath;
    }

    public void setTessDataPath(String tessDataPath) {
        this.tessDataPath = tessDataPath;
    }

}
